var api = getApp(), WxParse = require('../../wxParse/wxParse.js');;
Page({

    /**
     * 页面的初始数据
     */
    data: {
        articleInfo: [],
        isPlay: 1,
        musicShow: !1,
        article_id: '',
        praise_count: '',
        isZan: 0,
        commentArr: [],
        commentData: {},
        inputValue: ''

    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (o) {
        var a = this;
        wx.showLoading({ title: '加载中...' });
        a.data.article_id = o.article_id;
        wx.request({
            url: api.globalData.Api + '/articleInfo',
            method: 'GET',
            dataType: 'json',
            data: {
                articleId: o.article_id,
                u_id: o.user_id
            },
            success(res){
                let d = res.data.data;
                var e = wx.createInnerAudioContext();
                e.autoplay = true, e.loop = false, e.src = d.music_url;
                a.data.articleInfo = d, a.data.praise_count = d.good_count;
                a.data.isZan = d.is_good;
                a.setData({
                    articleInfo: d,
                    innerAudioContext: e,
                    praise_count: d.good_count,
                    isZan: d.is_good,
                    u_id: o.user_id
                });
                WxParse.wxParse('article', 'html', d.content, a, 0), wx.hideLoading();
            }
        }),wx.request({
            url: api.globalData.Api + '/articleComment',
            method: 'GET',
            data: { articleId: o.article_id },
            dataType: 'json',
            success(res){
                a.setData({ commentArr: res.data.data })
            }
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        // if (this.data.musicShow == true) {
        //     this.data.innerAudioContext.play();
        // }
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {
        this.data.innerAudioContext.pause();
    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {
        this.data.innerAudioContext.pause();
    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },
    // 文章点赞
    praiseArticel: function(){
        var a = this, u = wx.getStorageSync('userData'), a_id = a.data.article_id;
        wx.request({
            url: api.globalData.Api + '/articleGood',
            method: 'GET',
            data: {
                u_id: u.u_id,
                a_id: a_id
            },
            success(res){
                if (res.data.status == 1){
                    if (a.data.isZan == 0){
                        wx.showToast({
                            title: '点赞成功',
                            icon: 'success'
                        });
                        var z = a.data.praise_count + 1;
                        a.setData({
                            praise_count: z,
                            isZan: 1
                        })
                    }else{
                        wx.showToast({
                            title: '取消点赞成功',
                            icon: 'success'
                        });
                        var z = a.data.praise_count - 1;
                        a.setData({
                            praise_count: z,
                            isZan: !1
                        })
                    }
                }
            }
        })
    },
    // 文章投诉
    compLaint:function(){
        var a = this, u = wx.getStorageSync('userData'), a_id = a.data.article_id;
        wx.request({
            url: api.globalData.Api + '/complain',
            method: 'GET',
            dataType: 'json',
            data:{
                u_id: u.u_id,
                a_id: a_id
            },
            success(res){
                if (res.data.status == 1){
                    wx.showToast({
                        title: '投诉申请成功',
                        icon: 'success'
                    })
                }
            }
        })
    },
    // 文章评论

    addComment: function(o){
        var a = this, v = o.detail.value, u = wx.getStorageSync('userData');
        var p = a.data.commentData;
        p.u_id = u.u_id, p.a_id = a.data.article_id, p.content = v;
        if(v == ''){
            wx.showModal({
                title: '提示',
                content: '评论内容不能为空',
                showCancel: false
            })
        } else if (v > 100){
            wx.showModal({
                title: '提示',
                content: '评论字数不能超过100个',
                showCancel: false
            })
        }else{
            wx.request({
                url: api.globalData.Api + '/addComment',
                method: 'POST',
                data: { commentData: JSON.stringify(p) },
                header: api.header,
                dataType: 'json',
                success(res){
                    if (res.data.status == 1){
                        a.setData({
                            commentArr: res.data.data,
                            inputValue: ''
                        });
                    }
                }
            })
        }
        
        
    },
    // 音乐点击
    palyAudio:function(){
        if (this.data.isPlay){
            this.data.innerAudioContext.pause()
            this.setData({
                isPlay: !1
            })
        }else{
            this.data.innerAudioContext.play()
            this.setData({
                isPlay: 1,
            })
        }
    },
    // 开始创作
    goPost: function () {
        wx.switchTab({
            url: "../post/post"
        });
    },
    // 回到首页
    goHome: function () {
        wx.switchTab({
            url: "../discover/discover"
        });
    },
})